from django.shortcuts import render
from .models import Resources
# Create your views here.
from django.http import HttpResponse, Http404
from django.db.models import Avg
def index(request):
    return HttpResponse("不要说话")

def detail(request,num,num2):
    return HttpResponse("detail-%s-%s"%(num,num2))


def grades(request):
    qry = Resources.objects.distinct().values("name")
    for q in qry:
        oneavg = Resources.objects.filter(name=q['name'],level='1').aggregate(Avg('number'))
        print(oneavg,'oneavg',oneavg['number__avg'])
        if oneavg['number__avg'] == None:
            q['oneavg'] = 0
        else:
            q['oneavg'] =oneavg['number__avg']
        twoavg = Resources.objects.filter(name=q['name'],level='2').aggregate(Avg('number'))
        print(twoavg,'oneavg',twoavg['number__avg'])
        if twoavg['number__avg'] == None:
            q['twoavg'] = 0
        else:
            q['twoavg'] = twoavg['number__avg']
        threeavg = Resources.objects.filter(name=q['name'],level='3').aggregate(Avg('number'))
        print(threeavg,'oneavg',threeavg['number__avg'])
        if threeavg['number__avg'] == None:
            q['threeavg'] = 0
        else:
            q['threeavg'] = threeavg['number__avg']
        ywszavg = Resources.objects.filter(name=q['name']).aggregate(Avg('dianumber'))
        # print(oneavg,'oneavg',oneavg['number__avg'])
        q['ywszavg'] = ywszavg['dianumber__avg']

        q['jqf'] = q['oneavg']*0.2+q['twoavg']*0.3+q['threeavg']*0.5 #一级平均*20%+二级平均*30%+三级平均*50%
    print('qry',qry)
    return render(request, 'myApp/a.html', {"qry1": qry})